package com.kestrel.space.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.kestrel.space.common.utils.Generator;
import com.kestrel.space.common.utils.LargePage;
import com.kestrel.space.core.mapper.FileMappingUserMapper;
import com.kestrel.space.core.model.FileMappingOrg;
import com.kestrel.space.core.model.FileMappingUser;
import com.kestrel.space.core.query.FileMappingOrgQuery;
import com.kestrel.space.core.query.FileMappingUserQuery;
import com.kestrel.space.core.service.FileMappingUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author yebolin
 * @date 2024/9/20
 */
@Service
public class FileMappingUserServiceImpl implements FileMappingUserService {
    @Autowired
    private FileMappingUserMapper fileMappingUserMapper;

    @Override
    public List<FileMappingUser> listContent(FileMappingUserQuery query, LargePage page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
        }
        FileMappingUser fileMappingUser = BeanUtil.copyProperties(query, FileMappingUser.class);
        return fileMappingUserMapper.select(fileMappingUser);
    }

    @Override
    public String saveOrUpdate(FileMappingUser fileMappingUser) {
        String message = "插入成功";
        if (fileMappingUser.getId() == null) {
            fileMappingUser.setId(Generator.generateId());
            fileMappingUserMapper.insert(fileMappingUser);
        } else {
            message = "更新成功";
            fileMappingUserMapper.updateByPrimaryKeySelective(fileMappingUser);
        }
        return message;
    }

    @Override
    public String batchSaveOrUpdate(List<FileMappingUser> fileMappingUserList) {
        String message = "批量插入成功";
        if (CollectionUtils.isEmpty(fileMappingUserList)) {
            return "集合为空";
        }
        if(fileMappingUserList.get(0).getId() == null){
            for (FileMappingUser fileMappingUser : fileMappingUserList) {
                fileMappingUser.setId(Generator.generateId());
                fileMappingUserMapper.insert(fileMappingUser);
            }
        } else {
            message = "批量更新成功";
            for (FileMappingUser fileMappingUser : fileMappingUserList) {
                fileMappingUser.setId(Generator.generateId());
                fileMappingUserMapper.updateByPrimaryKey(fileMappingUser);
            }
        }
        return message;
    }
}
